import javax.sql.DataSource;

import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

public class JDBCDemo {
   /* public static void main(String[] args) throws SQLException {
        //JDBC 插入数据 java111 test
        //步骤比较多

        //创建数据源
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource) dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/java111?characterEncoding=utf8&useSSL=false");//向下转型
        ((MysqlDataSource) dataSource).setUser("root");
        ((MysqlDataSource) dataSource).setPassword("root");

        //和数据库服务器连接
        Connection connection =  dataSource.getConnection();

        Scanner scanner = new Scanner(System.in);
        //传入sql 并且
        System.out.print("输入学号id: ");
        int id = scanner.nextInt();
        System.out.println();
        System.out.print("输入姓名：");
        String name = scanner.next();

//        String sql = "insert into test values(id,'"+name+"')";
        String sql = "insert into test values(?,?)";//使用占位符 后面进行替换
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setInt(1,id);
        preparedStatement.setString(2,name);
        //执行 并返回
        int n = preparedStatement.executeUpdate();
        System.out.println(n+" 行收到了影响");

        //释放资源
        preparedStatement.close();
        connection.close();
    }*/


}
class Regist {
    public static void main(String[] args) throws SQLException {
        Regist r = new Regist();
        boolean result = r.regist("w123","1234");
        System.out.println(result);
    }
    public boolean regist(String username,String userpassword) throws SQLException {
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/buyticket?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("root");

        //链接数据库
        Connection connection = dataSource.getConnection();

        String sql = "insert into user values (?,?,?)";
        PreparedStatement preparedStatement= connection.prepareStatement(sql);

        preparedStatement.setString(1, "4");
        preparedStatement.setString(2, username);
        preparedStatement.setString(3, userpassword);

        int result = preparedStatement.executeUpdate();

        return result>0;
    }
}